/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView.dish;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;

import com.ruipengkj.commons.util.Util;
/**
 * 说明： 支付按钮
 */
public class PayJPopupMenu extends JPopupMenu implements ActionListener, MenuKeyListener {
	
	private static final long serialVersionUID = 1L;
	
	private JButton[] numButtons = new JButton[]{
			new JButton("1"), new JButton("2"), new JButton("3"), 
			new JButton("4"), new JButton("5"), new JButton("6"), 
			new JButton("7"), new JButton("8"), new JButton("9"), 
			new JButton("0"), new JButton("."), new JButton("←"),
	};//
	
	private JButton sureButton = new JButton("关  闭");//确定
	
	private JTextField payTF;
	
	public PayJPopupMenu(JTextField tf) {
		this.setSize(150, 200);
		this.setPreferredSize(new Dimension(250, 300));
		this.setBackground(Color.white);
		payTF = tf;
		init();
		this.addMenuKeyListener(this);
	}

	private void init() {
		JPanel jp = new JPanel(new GridLayout(4, 3, 5, 5));
		
		for (int i = 0, length = numButtons.length; i < length; ++i) {
			jp.add(numButtons[i]);
			numButtons[i].addActionListener(this);
			numButtons[i].setFont(Util.FONT_20_PLAIN);
		}
		
		JPanel jp2 = new JPanel();
		jp2.add(sureButton);
		Util.setSize(sureButton, 110, 40);
		sureButton.addActionListener(this);
		sureButton.setFont(Util.FONT_20_PLAIN);
		
		this.setLayout(new BorderLayout());
		this.add(jp);
		this.add(jp2, BorderLayout.SOUTH);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == sureButton) {
			this.setVisible(false);
		} else if (e.getSource() == numButtons[10]) {
			payTF.setText(payTF.getText()+numButtons[10].getText());
		} else if (e.getSource() == numButtons[11]) {
			if (payTF.getText().length() != 0)
				payTF.setText(payTF.getText().substring(0, payTF.getText().length()-1));
		} else {
			for (int i = 9; i >= 0; --i) {
				if (e.getSource() == numButtons[i]) {
					payTF.setText(payTF.getText()+numButtons[i].getText());
				}
			}
		}
	}

	@Override
	public void menuKeyPressed(MenuKeyEvent e) {
		if (e.getKeyCode() == MenuKeyEvent.VK_0) {
			numButtons[9].doClick();
		} else if (e.getKeyCode() == MenuKeyEvent.VK_1) {
			numButtons[0].doClick();
		} else if (e.getKeyCode() == MenuKeyEvent.VK_2) {
			numButtons[1].doClick();
		} else if (e.getKeyCode() == MenuKeyEvent.VK_3) {
			numButtons[2].doClick();
		} else if (e.getKeyCode() == MenuKeyEvent.VK_4) {
			numButtons[3].doClick();
		} else if (e.getKeyCode() == MenuKeyEvent.VK_5) {
			numButtons[4].doClick();
		} else if (e.getKeyCode() == MenuKeyEvent.VK_6) {
			numButtons[5].doClick();
		} else if (e.getKeyCode() == MenuKeyEvent.VK_7) {
			numButtons[6].doClick();
		} else if (e.getKeyCode() == MenuKeyEvent.VK_8) {
			numButtons[7].doClick();
		} else if (e.getKeyCode() == MenuKeyEvent.VK_9) {
			numButtons[8].doClick();
		} else if (e.getKeyCode() == MenuKeyEvent.VK_PERIOD) {
			numButtons[10].doClick();
		}  else if (e.getKeyCode() == MenuKeyEvent.VK_BACK_SPACE) {
			numButtons[11].doClick();
		}
	}

	@Override
	public void menuKeyReleased(MenuKeyEvent e) {
		
	}

	@Override
	public void menuKeyTyped(MenuKeyEvent e) {
		
	}
}